/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.admin;

import com.max.oti.system.model.TypeUser;
import com.max.oti.system.service.interfac.ITypeUserService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TypeUserBean implements Serializable {

    TypeUser typeUser = new TypeUser();
    @ManagedProperty("#{typeUserService}")
    ITypeUserService typeUserService;
    List<TypeUser> listTypeUser;
    List<TypeUser> filterListTypeUser;
    String idTypeUser;

    public void saveTypeUser() {
        if (typeUser.getId() == null) {
            try {
                typeUserService.save(typeUser);
                listTypeUser.add(typeUser);
                typeUser = new TypeUser();
                FacesUtil.addMessage("Registro con Exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception e) {
                FacesUtil.addMessage("Error al Registrar", null, FacesMessage.SEVERITY_FATAL);
            }
        } else {
            try {
                typeUserService.update(typeUser);
                FacesUtil.addMessage("Modificacion con Exito", null, FacesMessage.SEVERITY_INFO);
                typeUser = new TypeUser();
                listTypeUser = typeUserService.list();
            } catch (Exception ex) {
                FacesUtil.addMessage("Error en la Modificacion", null, FacesMessage.SEVERITY_INFO);
            }
        }
    }

    public void deleteTypeUser() {
        try {
            typeUserService.delete(typeUser);
            listTypeUser = typeUserService.list();//globalService.listHQL("FROM TypeUser");
            typeUser = new TypeUser();
        } catch (Exception ex) {
        }
    }
     
    public void onCellEdit(Object id, Object detail) {
        if (detail != null) {
            try {
                typeUserService.HQLexecute("UPDATE TypeUser tu SET tu.detail='" + detail + "' WHERE tu.id=" + id);
                FacesUtil.addMessage("Modificado con Exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                Logger.getLogger(TypeUserBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" METODOS GETTER Y SETTER ">
    public TypeUser getTypeUser() {
        return typeUser;
    }

    public void setTypeUser(TypeUser typeUser) {
        this.typeUser = typeUser;
    }

    public ITypeUserService getTypeUserService() {
        return typeUserService;
    }

    public void setTypeUserService(ITypeUserService typeUserService) {
        this.typeUserService = typeUserService;
    }

    public List<TypeUser> getListTypeUser() {
        if (listTypeUser == null) {
            try {
                listTypeUser = typeUserService.list();//globalService.listHQL("FROM TypeUser");
            } catch (Exception ex) {
            }
        }
        return listTypeUser;
    }

    public void setListTypeUser(List<TypeUser> listTypeUser) {
        this.listTypeUser = listTypeUser;
    }

    public List<TypeUser> getFilterListTypeUser() {
        return filterListTypeUser;
    }

    public void setFilterListTypeUser(List<TypeUser> filterListTypeUser) {
        this.filterListTypeUser = filterListTypeUser;
    }

    public String getIdTypeUser() {
        return idTypeUser;
    }

    public void setIdTypeUser(String idTypeUser) {
        this.idTypeUser = idTypeUser;
    }
    //</editor-fold> 
}
